package com.yunzo.coc.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Db;
import com.yunzo.coc.common.bean.BaseResponse;
import com.yunzo.coc.common.bean.Code;
import com.yunzo.coc.common.bean.DataResponse;
import com.yunzo.coc.common.bean.DatumResponse;
import com.yunzo.coc.common.bean.coc.GroupMemberConcernEntey;
import com.yunzo.coc.common.bean.coc.GroupMemberInbox;
import com.yunzo.coc.common.bean.coc.GroupMemberReplyInbox;
import com.yunzo.coc.common.bean.coc.SystemAdinfoEntey;
import com.yunzo.coc.common.bean.coc.SystemCollectEntey;
import com.yunzo.coc.common.bean.coc.SystemPushInfoCountEntey;
import com.yunzo.coc.common.bean.coc.SystemReviewEntey;
import com.yunzo.coc.common.bean.coc.SystemReviewRepEntey;
import com.yunzo.coc.common.bean.param.Collect;
import com.yunzo.coc.common.bean.param.Feedback;
import com.yunzo.coc.common.bean.param.MemberInbox;
import com.yunzo.coc.common.bean.param.MemberReplyInbox;
import com.yunzo.coc.common.bean.param.Review;
import com.yunzo.coc.common.Require;
import com.yunzo.coc.common.utils.DateUtils;
import com.yunzo.coc.common.utils.JsonObjectUtil;
import com.yunzo.coc.common.utils.RandomUtils;
import com.yunzo.coc.interceptor.TokenInterceptor;
import com.yunzo.coc.model.BasicApploginModel;
import com.yunzo.coc.model.BasicFormActioyModel;
import com.yunzo.coc.model.GroupAppinfoModel;
import com.yunzo.coc.model.GroupMemberConcernModel;
import com.yunzo.coc.model.GroupMemberFeedbackModel;
import com.yunzo.coc.model.GroupMemberInboxModel;
import com.yunzo.coc.model.GroupMemberInboxReplyModel;
import com.yunzo.coc.model.GroupMemberModel;
import com.yunzo.coc.model.GroupMemberShowtimeModel;
import com.yunzo.coc.model.GroupMemberinfoModel;
import com.yunzo.coc.model.SystemAdinfoModel;
import com.yunzo.coc.model.SystemCollectModel;
import com.yunzo.coc.model.SystemJpushUrlInfoModel;
import com.yunzo.coc.model.SystemJpushUserReadInfoModel;
import com.yunzo.coc.model.SystemPointlikeModel;
import com.yunzo.coc.model.SystemReviewModel;
import com.yunzo.coc.model.SystemReviewReplyModel;
import com.yunzo.coc.version.Version;
import com.yunzo.coc.version.VersionManager;

/**
 * 公共模块的api*
 * 
 * 意见反馈: POST /api/feedback 用户点赞及取消接口 /api/userproetformid 版本更新检查: GET
 * /api/version/check
 *
 * @author xiaobo
 */
public class CommonAPIController extends BaseAPIController {

	/**
	 * 会员意见反馈
	 */
	public void feedback() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			renderJson(new BaseResponse(Code.NOT_FOUND));
			return;
		}
		Feedback fb = null;
		String feedback = null,mids=null,groupid=null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), Feedback.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			mids=fb.getMid();
			groupid=fb.getGroupid();
			feedback = fb.getFeedback();
		}else
			return;

		// 校验必填项参数 根据业务需求决定
		if (!notNull(Require.me().put(mids, "mids can not be null")
				.put(groupid, "groupid can not be null")
				.put(feedback, "feedback can not be null"))) {
			return;
		}

		GroupMemberFeedbackModel feedBack = new GroupMemberFeedbackModel()
				.set(GroupMemberFeedbackModel.ID, getCreateUUId())
				.set(GroupMemberFeedbackModel.VERSION, 0)
				.set(GroupMemberFeedbackModel.ORDERIDS, 0)
				.set(GroupMemberFeedbackModel.FEEDBACK, feedback)
				.set(GroupMemberFeedbackModel.MID, mids)
				.set(GroupMemberFeedbackModel.GROUPID, groupid)
				.set(GroupMemberFeedbackModel.CREATETIME, DateUtils.getDate());
		// 保存反馈
		boolean flag = feedBack.save();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));
	}

	/**
	 * 用户点赞及取消接口 /api/userproetformid
	 */
	public void userproetformid() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 登录帐号
		String GID = getPara("groupid");// 团体ID
		String PID = getPara("pid");// 业务ID
		String DID = getPara("did");// 类型ID
		int TYPE = getParaToInt("type", 0);// 操作值

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(GID, "groupid can not be null")
				.put(PID, "pid can not be null")
				.put(DID, "did can not be null")
				.put(TYPE, "type can not be null"))) {
			return;
		}
		// 保存数据
		boolean flag = false;
		if (TYPE == 0) {
			flag = new SystemPointlikeModel()
					.set(SystemPointlikeModel.ID, getCreateUUId())
					.set(SystemPointlikeModel.VERSION, 0)
					.set(SystemPointlikeModel.ORDERIDS, 0)
					.set(SystemPointlikeModel.GROUPID, GID)
					.set(SystemPointlikeModel.MID, MID)
					.set(SystemPointlikeModel.PIDS, PID)
					.set(SystemPointlikeModel.PTYPE, DID)
					.set(SystemPointlikeModel.PVALUE, TYPE)
					.set(SystemPointlikeModel.CREATETIME, DateUtils.getDate())
					.save();
		} else {
			// 删除用户点赞记录
			if (Db.update(DEL_POINTLIKE_SQL, GID, DID, PID, MID) != 0) {
				flag = true;
			}
		}
		// 统计条数
		SystemPointlikeModel spm = SystemPointlikeModel.dao.findFirst(
				GET_POINTLIKE_SUMNUMBER_SQL, GID, DID, PID);
		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL, flag ? ""
				+ (spm != null ? spm.getNumber(SystemPointlikeModel.SCOUNT)
						.intValue() : 0) : Code.FINAL_STRING_ERROR));
	}

	/**
	 * 获取评论列表 /api/getbussdatareviewbypid
	 */
	public void getbussdatareviewbypidforlist() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String PID = getPara("pid");// 业务ID
		String DID = getPara("did");// 类型ID
		int SIZE = getParaToInt("pagesize", 0);// 分页大小

		// 校验必填项参数
		if (!notNull(Require.me().put(PID, "pid can not be null")
				.put(DID, "did can not be null")
				.put(SIZE, "pagesize can not be null"))) {
			return;
		}
		List<SystemReviewModel> srmodellist = SystemReviewModel.dao.find(
				SYSTEMREVIEWPIDDIDSIZESQL, PID, DID, SIZE);
		List<SystemReviewEntey> reslist = null;
		if (srmodellist != null) {
			reslist = new ArrayList<SystemReviewEntey>();
			SystemReviewEntey sr = null;
			//GroupMemberinfoModel gmm = null;
			GroupMemberModel nowUser = null;//定义会员基础
			SystemReviewReplyModel srrm = null;
			for (SystemReviewModel sysr : srmodellist) {
				sr = new SystemReviewEntey();
			//	gmm = GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,
						//sysr.getStr(SystemReviewModel.MID));
				//通过mid查询会员基础数据
				nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						sysr.getStr(SystemReviewModel.MID));
				sr.setN0(sysr.getStr(SystemReviewModel.ID));
//				sr.setN1(gmm.getStr(GroupMemberinfoModel.REALNAME));
				sr.setN1(nowUser.getStr(GroupMemberModel.REALNAME));
				sr.setN2(sysr.getStr(SystemReviewModel.MID));
//				sr.setN3(gmm.getStr(GroupMemberinfoModel.HEADIMAGE));
				sr.setN3(nowUser.getStr(GroupMemberModel.HEADIMAGE));
				sr.setN4(sysr.getStr(SystemReviewModel.RVALUE));
				sr.setN5(DateUtils.formatDate(sysr
						.getDate(SystemReviewModel.CREATETIME)));
				srrm = SystemReviewReplyModel.dao.findFirst(
						GET_REVIEW_REP_SUMNUMBER_SQL,
						sysr.getStr(SystemReviewModel.ID));
				sr.setN6(srrm != null ? srrm.getNumber(
						SystemReviewReplyModel.SCOUNT).intValue() : 0);

				reslist.add(sr);
			}
		}
		DataResponse response = new DataResponse();
		response.setCode(srmodellist != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(srmodellist != null ? "SUCCESS" : "无评论列表");
		response.setData(reslist);
		renderJson(response);
	}

	/**
	 * 发表评论 /api/sendreviewbymid
	 */
	public void sendreviewbymid() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		Review fb = null;
		String MID =null,GID=null,DID=null, PID = null,comments=null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), Review.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			MID=fb.getMid();
			GID=fb.getGroupid();
			DID=fb.getDid();
			PID = fb.getPid();
			comments = fb.getCntvalue();
		}else
			return;

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(GID, "groupid can not be null")
				.put(PID, "pid can not be null")
				.put(DID, "did can not be null")
				.put(comments, "conment can not be null"))) {
			return;
		}
		// 保存数据
		boolean flag = new SystemReviewModel()
				.set(SystemReviewModel.ID, getCreateUUId())
				.set(SystemReviewModel.VERSION, 0)
				.set(SystemReviewModel.ORDERIDS, 0)
				.set(SystemReviewModel.GROUPID, GID)
				.set(SystemReviewModel.MID, MID)
				.set(SystemReviewModel.REVIEWID, PID)
				.set(SystemReviewModel.RTYPE, DID)
				.set(SystemReviewModel.RVALUE, comments)
				.set(SystemReviewModel.CREATETIME, DateUtils.getDate()).save();

		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));

	}

	/**
	 * 获取回复评论 列表 /api/getreplyreviewbymidforlist
	 */
	public void getreplyreviewbymidforlist() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String PID = getPara("pid");// 评论ID
		int SIZE = getParaToInt("pagesize", 0);// 分页大小

		// 校验必填项参数
		if (!notNull(Require.me().put(PID, "pid can not be null")
				.put(SIZE, "pagesize can not be null"))) {
			return;
		}

		List<SystemReviewReplyModel> replylist = SystemReviewReplyModel.dao
				.find(SYSTEMREVIEWREPLERIDSQL, PID, SIZE);
		List<SystemReviewRepEntey> listsrr = null;
		if (replylist != null) {
			listsrr = new ArrayList<SystemReviewRepEntey>();
			SystemReviewRepEntey srrentey = null;
			//GroupMemberinfoModel gmm = null;
			GroupMemberModel nowUser = null;//定义会员基础
			
			for (SystemReviewReplyModel sysrr : replylist) {
				srrentey = new SystemReviewRepEntey();
				srrentey.setN0(sysrr.getStr(SystemReviewReplyModel.FROMMIDS));
			//	gmm = GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,
						//sysrr.getStr(SystemReviewReplyModel.FROMMIDS));
				//通过frommids(回复人手机号码)查询会员基础数据
				nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						sysrr.getStr(SystemReviewReplyModel.FROMMIDS));
				
//				srrentey.setN1(gmm.getStr(GroupMemberinfoModel.REALNAME));
				srrentey.setN1(nowUser.getStr(GroupMemberModel.REALNAME));
				srrentey.setN2(sysrr.getStr(SystemReviewReplyModel.TOMIDS));
			//	gmm = GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,
						//sysrr.getStr(SystemReviewReplyModel.TOMIDS));
				nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						sysrr.getStr(SystemReviewReplyModel.TOMIDS));
//				srrentey.setN3(gmm.getStr(GroupMemberinfoModel.REALNAME));
				srrentey.setN3(nowUser.getStr(GroupMemberModel.REALNAME));
				srrentey.setN4(sysrr.getStr(SystemReviewReplyModel.REPLYVALUE));
				srrentey.setN5(DateUtils.formatDate(sysrr
						.getDate(SystemReviewReplyModel.CREATETIME)));

				listsrr.add(srrentey);
			}
		}

		DataResponse response = new DataResponse();
		response.setCode(replylist != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(replylist != null ? "SUCCESS" : "无评论回复列表");
		response.setData(listsrr);
		renderJson(response);
	}

	/**
	 * 回复评论 /api/replyreviewbymid
	 */
	public void replyreviewbymid() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String TOMIDS = getPara("tomids");
		Review fb = null;
		String FROMMID=null,PID = null,DID=null,comments=null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), Review.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			FROMMID=fb.getMid();
			DID=fb.getDid();
			PID = fb.getPid();
			comments = fb.getCntvalue();
		}else
			return;
		// 校验必填项参数
		if (!notNull(Require.me().put(FROMMID, "mid can not be null")
				.put(TOMIDS, "tomids can not be null")
				.put(PID, "pid can not be null")
				.put(comments, "conment can not be null"))) {
			return;
		}
		// 保存数据
		boolean flag = new SystemReviewReplyModel()
				.set(SystemReviewModel.ID, getCreateUUId())
				.set(SystemReviewReplyModel.VERSION, 0)
				.set(SystemReviewReplyModel.ORDERIDS, 0)
				.set(SystemReviewReplyModel.FROMMIDS, FROMMID)
				.set(SystemReviewReplyModel.TOMIDS, TOMIDS)
				.set(SystemReviewReplyModel.RID, PID)
				.set(SystemReviewReplyModel.REPLYVALUE, comments)
				.set(SystemReviewReplyModel.CREATETIME, DateUtils.getDate())
				.save();

		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));

	}

	/**
	 * 获取关注列表 /api/getattentionforlist
	 */
	public void getattentionforlist() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 会员手机号码
		String GROUPID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型
		int SIZE = getParaToInt("pagesize", 0);// 分页大小

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(DID, "did can not be null")
				.put(SIZE, "pagesize can not be null"))) {
			return;
		}

		List<GroupMemberConcernModel> conlist = GroupMemberConcernModel.dao
				.find(SYSTEMCONCERNBYMIDSQL, MID, DID, SIZE);
		List<GroupMemberConcernEntey> gmconlist = null;
		if (conlist != null) {
			gmconlist = new ArrayList<GroupMemberConcernEntey>();
			GroupMemberConcernEntey gmcon = null;
			//GroupMemberinfoModel gminfo = null;
			GroupMemberShowtimeModel gmshowtime = null;
			GroupMemberModel nowUser = null;//定义会员基础
			
			for (GroupMemberConcernModel gmcc : conlist) {
				gmcon = new GroupMemberConcernEntey();
				gmcon.setN0(gmcc.getStr(GroupMemberConcernModel.TOMID));
			//	gminfo = GroupMemberinfoModel.dao.findFirst(MEMBERMIDSSQL,
						//gmcc.getStr(GroupMemberConcernModel.TOMID));
				//通过tomid(被关注会员手机号码)查询会员基础数据
				nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						gmcc.getStr(GroupMemberConcernModel.TOMID));
//				gmcon.setN1(gminfo.getStr(GroupMemberinfoModel.HEADIMAGE));
//				gmcon.setN2(gminfo.getStr(GroupMemberinfoModel.REALNAME));
				gmcon.setN1(nowUser.getStr(GroupMemberModel.HEADIMAGE));
				gmcon.setN2(nowUser.getStr(GroupMemberModel.REALNAME));
				gmshowtime = GroupMemberShowtimeModel.dao.findFirst(
						MEMBERSHOWTIMEFORGROUPIDANDMIDSANDCREATETIMESQL,
						GROUPID, MID,
						gmcc.getDate(GroupMemberConcernModel.CREATETIME));
				gmcon.setN3(gmshowtime
						.getStr(GroupMemberShowtimeModel.SHOWCOMMENT));

				gmconlist.add(gmcon);
			}
		}

		DataResponse response = new DataResponse();
		response.setCode(conlist != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(conlist != null ? "SUCCESS" : "无关注列表");
		response.setData(gmconlist);
		renderJson(response);
	}

	/**
	 * 关注 /api/attentiontomid
	 */
	public void attentiontomid() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 用户手机号码
		String GID = getPara("groupid");// 团体ID
		String PID = getPara("pid");// 业务ID
		String DID = getPara("did");// 类型ID
		String TOMID = getPara("tomid");// 被关注人手机号码
		int type = getParaToInt("type", 0);// 类型

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(GID, "groupid can not be null")
				.put(PID, "pid can not be null")
				.put(DID, "did can not be null")
				.put(type, "type can not be null")
				.put(TOMID, "tomid can not be null"))) {
			return;
		}
		// 保存数据
		boolean flag = false;
		if (type == 0) {
			flag =new GroupMemberConcernModel()
			.set(GroupMemberConcernModel.ID, getCreateUUId())
			.set(GroupMemberConcernModel.VERSION, 0)
			.set(GroupMemberConcernModel.ORDERIDS, 0)
			.set(GroupMemberConcernModel.GROUPID, GID)
			.set(GroupMemberConcernModel.FROMMID, MID)
			.set(GroupMemberConcernModel.CONCOMMENTS, PID)
			.set(GroupMemberConcernModel.DIDS, DID)
			.set(GroupMemberConcernModel.TOMID, TOMID)
			.set(GroupMemberConcernModel.CREATETIME, DateUtils.getDate())
			.save();
		} else {
			// 删除用户点赞记录
			if (Db.update(DEL_CONNECT_SQL, GID, DID, PID, MID,TOMID) != 0) {
				flag = true;
			}
		}

		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));

	}

	/**
	 * 获取收藏列表 /api/getcollectlist
	 */
	public void getcollectlist() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 会员手机号码
		String GROUPID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型
		int SIZE = getParaToInt("pagesize", 0);// 分页大小

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(DID, "did can not be null")
				.put(GROUPID, "groupid can not be null")
				.put(SIZE, "pagesize can not be null"))) {
			return;
		}
		List<SystemCollectModel> conlist = SystemCollectModel.dao.find(
				SYSTEMCOLLECTBYMIDANDDIDSQL, MID, DID,GROUPID, SIZE);
		List<SystemCollectEntey> gmconlist = null;
		if (conlist != null) {
			SystemCollectEntey gmcon = null;
			gmconlist = new ArrayList<SystemCollectEntey>();
			for (SystemCollectModel gmcc : conlist) {
				gmcon = new SystemCollectEntey();
				gmcon.setN0(gmcc.getStr(SystemCollectModel.CIDS));
				gmcon.setN1(gmcc.getStr(SystemCollectModel.CVALUE));

				gmconlist.add(gmcon);
			}

		}
		DataResponse response = new DataResponse();
		response.setCode(conlist != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(conlist != null ? "SUCCESS" : "无收藏列表");
		response.setData(gmconlist);
		renderJson(response);
	}

	/**
//	 * 收藏操作 /api/collectforall
	 */
	public void collectforall() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		int type = 0;// 类型（0收藏，1取消收藏）
		String MID=null,GID=null,DID=null,PID = null,comments=null;// 业务ID
		Collect fb = null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), Collect.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			MID=fb.getMid();
			GID=fb.getGroupid();
			DID=fb.getDid();
			type=fb.getType();
			PID = fb.getPid();
			comments = fb.getCollectinfo();
		}else
			return;
		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(GID, "groupid can not be null")
				.put(DID, "did can not be null")
				.put(type, "type can not be null"))) {
			return;
		}
		
		boolean flag=false;
		if(type==0)
		{
			// 保存数据
			flag = new SystemCollectModel()
			.set(SystemCollectModel.ID, getCreateUUId())
			.set(SystemCollectModel.VERSION, 0)
			.set(SystemCollectModel.ORDERIDS, 0)
			.set(SystemCollectModel.GROUPID, GID)
			.set(SystemCollectModel.MID, MID)
			.set(SystemCollectModel.CIDS, PID)
			.set(SystemCollectModel.CTYPE, DID)
			.set(SystemCollectModel.CVALUE, comments)
			.set(SystemCollectModel.CREATETIME, DateUtils.getDate()).save();
		}else
		{
			// 删除用户点赞记录
			if (Db.update(DEL_COLLECT_SQL, GID, MID, DID,PID) != 0) {
				flag = true;
			}
			
		}
		
		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));

	}

	/**
	 * 获取投放广告展示图片集合 /api/getadimagebymidlist
	 */
	public void getadimagebymidlist() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 用户手机号码
		String GID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型ID
		int TYPE = getParaToInt("adtype", 0);// 投放类型

		// 校验必填项参数
		if (!notNull(Require.me().put(GID, "groupid can not be null")
				.put(DID, "did can not be null")
				.put(TYPE, "adtype can not be null"))) {
			return;
		}
		List<SystemAdinfoModel> conlist = SystemAdinfoModel.dao.find(
				SYSTEMADINFOBYTYPESQL, TYPE,GID);
		List<SystemAdinfoEntey> gmconlist = null;
		if (conlist != null) {
			gmconlist = new ArrayList<SystemAdinfoEntey>();
			if(conlist.size()==0)
			{
				conlist = SystemAdinfoModel.dao.find(
					SYSTEMADINFOBYTYPESQL, TYPE,"88888888888888888888888888888888");
			}
			SystemAdinfoEntey gmcon = null;
			for (SystemAdinfoModel gmcc : conlist) {
				gmcon = new SystemAdinfoEntey();
				gmcon.setN0(gmcc.getStr(SystemAdinfoModel.ID));
				gmcon.setN1(gmcc.getStr(SystemAdinfoModel.GADTITLE));
				gmcon.setN2(gmcc.getStr(SystemAdinfoModel.GADLOGO));
				gmcon.setN3(gmcc.getNumber(SystemAdinfoModel.GADKEY).intValue());
				gmconlist.add(gmcon);
			}
		}
		BasicApploginModel balog=BasicApploginModel.dao.findFirst("SELECT createtime  from y_basic_applogin  WHERE groupid='"+GID+"' and usertell='"+MID+"' ORDER BY createtime desc limit 1");
		int flag=balog!=null?DateUtils.getDateDaySpace(balog.getDate(BasicApploginModel.CREATETIME),DateUtils.getDate()):0;
		if(flag>0)
		{
			//保存登陆记录
			new BasicApploginModel().set(BasicApploginModel.ID, RandomUtils.randomCustomUUID())
			.set(BasicApploginModel.VERSION, 0)
			.set(BasicApploginModel.MID, MID)
			.set(BasicApploginModel.MIDTYPE, MID.equals("13500000000")?1:0)
			.set(BasicApploginModel.GROUPID, DID)
			.set(BasicApploginModel.CREATETIME, DateUtils.getDate()).save();
		}
		DataResponse response = new DataResponse();
		response.setCode(conlist != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(conlist != null ? "SUCCESS" : "无广告信息表");
		response.setData(gmconlist);
		renderJson(response);
	}

	/**
	 * 获取投放广告详情 /api/getadinfobyadids
	 */
	public void getadinfobyadids() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("adid");// 广告ID
		String GID = getPara("groupid");// 团体ID
		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "adid can not be null"))) {
			return;
		}
		SystemAdinfoModel SAM = null;
		if (GID != null) {
			SAM = SystemAdinfoModel.dao.findFirst(SYSTEMADINFOBYGIDANDAIDSQL,
					GID, MID);
		} else
			SAM = SystemAdinfoModel.dao.findById(MID);
		boolean flag = SAM != null ? true : false;

		// 返回数据
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? SAM.getStr(SystemAdinfoModel.GADVALUE)
						: Code.FINAL_STRING_ERROR));
	}

	/**
	 * 发送的私信 /api/sendinboxinfo
	 */
	public void sendinboxinfo() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid=null,mid=null,did=null, tomids=null,commnet=null;
		MemberInbox fb = null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), MemberInbox.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			groupid=fb.getGroupid();
			mid=fb.getMid();
			did=fb.getDid();
			tomids=fb.getTomids();
			commnet=fb.getCommnet();
		}else
			return;
		
		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(did, "did can not be null")
				.put(groupid, "groupid can not be null"))) {
			return;
		}
		
		boolean flag = new GroupMemberInboxModel()
				.set(GroupMemberInboxModel.ID, getCreateUUId())
				.set(GroupMemberInboxModel.ORDERIDS, 0)
				.set(GroupMemberInboxModel.VERSION, 0)
				.set(GroupMemberInboxModel.GROUPID, groupid)
				.set(GroupMemberInboxModel.SENDMID, mid)
					.set(GroupMemberInboxModel.INTYPE, did)
				.set(GroupMemberInboxModel.TOMID, tomids)
				.set(GroupMemberInboxModel.INBOXCOMMNET, commnet)
				.set(GroupMemberInboxModel.CREATETIME, DateUtils.getDate())
				.save();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));

	}

	/**
	 * 回复我的私信 /api/replymyinboxinfo
	 */
	public void replymyinboxinfo() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String mid=null,groupid=null,did=null,inboxid=null,tomids=null,commnet=null;// 团体ID
		MemberReplyInbox fb = null;
		try {
			fb = getRepuestJosnObject(getRequest().getReader(), MemberReplyInbox.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (fb != null) {
			mid=fb.getMid();
			did=fb.getDid();
			groupid=fb.getGroupid();
			inboxid=fb.getInboxid();
			tomids=fb.getTomids();
			commnet=fb.getCommnet();
		}else
			return;
		
		
		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(did, "did can not be null")
				.put(groupid, "groupid can not be null"))) {
			return;
		}
		
		boolean flag = new GroupMemberInboxReplyModel()
				.set(GroupMemberInboxReplyModel.ID, getCreateUUId())
				.set(GroupMemberInboxReplyModel.ORDERIDS, 0)
				.set(GroupMemberInboxReplyModel.VERSION, 0)
				.set(GroupMemberInboxReplyModel.INBOXID, inboxid)
				.set(GroupMemberInboxReplyModel.TOREPLYMID, tomids)
				.set(GroupMemberInboxReplyModel.REPLYMID, mid)
				.set(GroupMemberInboxReplyModel.REPLYCOMMNET, commnet)
				.set(GroupMemberInboxReplyModel.CREATETIME, DateUtils.getDate())
				.save();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));
	}

	/**
	 * 删除我的私信 /api/delmyinboxinfobyid
	 */
	public void delmyinboxinfobyid() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");// 类型ID
		String inboxid = getPara("inboxid");// 私信ID

		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(did, "did can not be null")
				.put(inboxid, "inboxid can not be null")
				.put(groupid, "groupid can not be null"))) {
			return;
		}
		boolean flag = GroupMemberInboxModel.dao.deleteById(inboxid);
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,
				flag ? Code.FINAL_STRING_SUCCESS : Code.FINAL_STRING_ERROR));
	}
	/**
	 * 获取我的私信列表  /api/delmyinboxinfobyid
	 */
	public void getinboxinfolistbymid()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");// 类型ID
		String tomids=getPara("tomids");//
		int pagesize = getParaToInt("pagesize", 0);// 投放类型
		int curCount= getParaToInt("curCount", 0);// 当前数据量

		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(did, "did can not be null")
				.put(pagesize, "pagesize can not be null")
				.put(curCount, "curCount can not be null")
				.put(groupid, "groupid can not be null"))) {
			return;
		}
		List<GroupMemberInbox> Listinbox=null;
		GroupMemberInbox inbox=null;
		List<GroupMemberReplyInbox> listreinbox=null;
		GroupMemberReplyInbox reinbox=null;
		List<GroupMemberInboxModel> listmodelinbox=null;
		if(tomids!=null)
		{
			listmodelinbox=GroupMemberInboxModel.dao.find(GROUPMEMBER_INBOX_TOM_LIST_SQL, groupid,tomids,mid,did,curCount,curCount+pagesize);
		}else
		{
			listmodelinbox=GroupMemberInboxModel.dao.find(GROUPMEMBER_INBOX_LIST_SQL, groupid,mid,did,curCount,curCount+pagesize);
		}

		if(listmodelinbox!=null)
		{
			Listinbox=new ArrayList<GroupMemberInbox>();
			for(GroupMemberInboxModel gmim:listmodelinbox)
			{
				inbox=new GroupMemberInbox();
//				GroupMemberinfoModel userinfo=GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,
//						gmim.getStr(GroupMemberInboxModel.SENDMID));
				//通过sendmid(业务IDS)查询会员基础数据
				GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						gmim.getStr(GroupMemberInboxModel.SENDMID));
				inbox.setN0(gmim.getStr(GroupMemberInboxModel.ID));
				inbox.setN1(nowUser!=null?nowUser.getStr(GroupMemberModel.REALNAME):"");
				inbox.setN2(nowUser!=null?nowUser.getStr(GroupMemberModel.HEADIMAGE):"");
				inbox.setN3(gmim.getStr(GroupMemberInboxModel.SENDMID));
				inbox.setN4(gmim.getStr(GroupMemberInboxModel.INBOXCOMMNET));
				inbox.setN5(DateUtils.formatDate(gmim.getDate(GroupMemberInboxModel.CREATETIME)));
				List<GroupMemberInboxReplyModel> replymodellist=GroupMemberInboxReplyModel.dao.find(GROUPMEMBER_REPLY_INBOX_LIST_SQL, gmim.getStr(GroupMemberInboxModel.ID));
				if(replymodellist!=null)
				{
					listreinbox=new ArrayList<GroupMemberReplyInbox>();
					inbox.setN6(replymodellist.size());
					for(GroupMemberInboxReplyModel rep:replymodellist)
					{
						reinbox=new GroupMemberReplyInbox();
						reinbox.setN0(rep.getStr(GroupMemberInboxReplyModel.TOREPLYMID));
						reinbox.setN1(rep.getStr(GroupMemberInboxReplyModel.REPLYMID));
						reinbox.setN2(rep.getStr(GroupMemberInboxReplyModel.REPLYCOMMNET));
						reinbox.setN3(DateUtils.formatDate(rep.getDate(GroupMemberInboxReplyModel.CREATETIME)));
						
						listreinbox.add(reinbox);
					}
					inbox.setN7(listreinbox);
				}else
				{
					inbox.setN6(0);
					inbox.setN7(null);
				}
				Listinbox.add(inbox);
			}
			
		}
		
		DataResponse response = new DataResponse();
		response.setCode(listmodelinbox != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(listmodelinbox != null ? "SUCCESS" : "无私信信息");
		response.setData(Listinbox);
		renderJson(response);
	}
	/**
	 * 获取我的私信列表  /api/delmyinboxinfobyid
	 */
	public void getmysendinboxinfolistbymid()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");// 类型ID
		int pagesize = getParaToInt("pagesize", 0);// 投放类型
		int curCount= getParaToInt("curCount", 0);// 当前数据量

		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(did, "did can not be null")
				.put(pagesize, "pagesize can not be null")
				.put(curCount, "curCount can not be null")
				.put(groupid, "groupid can not be null"))) {
			return;
		}
		List<GroupMemberInbox> Listinbox=null;
		GroupMemberInbox inbox=null;
		List<GroupMemberReplyInbox> listreinbox=null;
		GroupMemberReplyInbox reinbox=null;
		List<GroupMemberInboxModel> listmodelinbox=GroupMemberInboxModel.dao.find(GROUPMEMBER_SEND_INBOX_LIST_SQL, groupid,mid,did,curCount,curCount+pagesize);
	
		if(listmodelinbox!=null)
		{
			Listinbox=new ArrayList<GroupMemberInbox>();
			for(GroupMemberInboxModel gmim:listmodelinbox)
			{
				inbox=new GroupMemberInbox();

				GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,
						gmim.getStr(GroupMemberInboxModel.TOMID));
				inbox.setN0(gmim.getStr(GroupMemberInboxModel.ID));
				inbox.setN1(nowUser!=null?nowUser.getStr(GroupMemberModel.REALNAME):"");
				inbox.setN2(nowUser!=null?nowUser.getStr(GroupMemberModel.HEADIMAGE):"");
				inbox.setN3(gmim.getStr(GroupMemberInboxModel.SENDMID));
				inbox.setN4(gmim.getStr(GroupMemberInboxModel.INBOXCOMMNET));
				inbox.setN5(DateUtils.formatDate(gmim.getDate(GroupMemberInboxModel.CREATETIME)));
				List<GroupMemberInboxReplyModel> replymodellist=GroupMemberInboxReplyModel.dao.find(GROUPMEMBER_REPLY_INBOX_LIST_SQL, gmim.getStr(GroupMemberInboxModel.ID));
				if(replymodellist!=null)
				{
					listreinbox=new ArrayList<GroupMemberReplyInbox>();
					inbox.setN6(replymodellist.size());
					for(GroupMemberInboxReplyModel rep:replymodellist)
					{
						reinbox=new GroupMemberReplyInbox();
						reinbox.setN0(rep.getStr(GroupMemberInboxReplyModel.TOREPLYMID));
						reinbox.setN1(rep.getStr(GroupMemberInboxReplyModel.REPLYMID));
						reinbox.setN2(rep.getStr(GroupMemberInboxReplyModel.REPLYCOMMNET));
						reinbox.setN3(DateUtils.formatDate(rep.getDate(GroupMemberInboxReplyModel.CREATETIME)));
						
						listreinbox.add(reinbox);
					}
					inbox.setN7(listreinbox);
				}else
				{
					inbox.setN6(0);
					inbox.setN7(null);
				}
				Listinbox.add(inbox);
			}
			
		}
		
		DataResponse response = new DataResponse();
		response.setCode(listmodelinbox != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(listmodelinbox != null ? "SUCCESS" : "无私信信息");
		response.setData(Listinbox);
		renderJson(response);
	}
	public void postformdata()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String isjoin=getPara("isjoin");
		String isfoot=getPara("isfoot");
		String ren=getPara("ren");
		
		boolean flag =new BasicFormActioyModel().set(BasicFormActioyModel.ID, getCreateUUId())
		.set(BasicFormActioyModel.ISJOIN, isjoin).set(BasicFormActioyModel.ISFOOT, isfoot).set(BasicFormActioyModel.FOOTNUM, ren).set(BasicFormActioyModel.DATETIME, new Date()).save();
		render("return.html");
		//renderJson(new BaseResponse("http://www.rabbitpre.com/m/ZBFbUbf"));
		
	}
	/**
	 * 获取APP版本信息 /api/getappversioninfo
	 */
	public void getappversioninfo() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");// 类型ID
		// 校验必填项参数
		if (!notNull(Require.me().put(groupid, "groupid can not be null"))) {
			return;
		}

		GroupAppinfoModel gam = GroupAppinfoModel.dao.findFirst(
				GROUPAPP_INFO_SQL, groupid);
		renderJson(new BaseResponse(
				gam != null ? gam.getStr(GroupAppinfoModel.APPVERSION)
						: Code.FINAL_STRING_APP));

	}

	@ActionKey("/api/yunzo/userprot")
	public void getUserYunzoinfo() {
		renderJson(new BaseResponse("/app/userprot.html"));
	}

	@ActionKey("/api/yunzo/aboutinfo")
	public void getAboutYunzoinfo() {
		renderJson(new BaseResponse("/app/gyzz.html"));
	}

	@ActionKey("/api/yunzo/getAppurl")
	public void getAppurl() {
		renderJson(new BaseResponse("/app/userprot.html"));
	}
	
	/**
	 * 推送请求相关网页
	 */
	@ActionKey("/api/yunzo/getJPushAppurl")
	public void getJPushAppurl() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String infoid = getPara("infoid");// 推送IDS
		String mid = getPara("mid");// 会员手机号码
		
		// 校验必填项参数
		if (!notNull(Require.me().put(infoid, "infoid can not be null").put(mid, "mid can not be null"))) {
			return;
		}
	
		new SystemJpushUserReadInfoModel().set(SystemJpushUserReadInfoModel.ID, getCreateUUId())
		.set(SystemJpushUserReadInfoModel.MIDS, mid)
		.set(SystemJpushUserReadInfoModel.PUSHIDS, infoid).set(SystemJpushUserReadInfoModel.CREATETIME, DateUtils.getDate()).save();
		
		SystemJpushUrlInfoModel sjuim=SystemJpushUrlInfoModel.dao.findById(infoid);
		SystemCollectEntey sce=null;
		if(sjuim!=null)
		{
			sce=new SystemCollectEntey();
			sce.setN0(sjuim.getStr(SystemJpushUrlInfoModel.WEBURL));
			sce.setN1(sjuim.getStr(SystemJpushUrlInfoModel.REQPARAM));
		}
		DatumResponse response = new DatumResponse();
		response.setCode(sjuim != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(sjuim != null ? "SUCCESS" : "没有对应详情");
		response.setDatum(sce);
		renderJson(response);
	}
	/**
	 * 推送请求相关网页
	 */
	@ActionKey("/api/yunzo/getMyReadNewInfoforFlag")
	public void getMyReadNewInfoforFlag() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");// 类型ID
		
		// 校验必填项参数
		if (!notNull(Require.me().put(groupid, "groupid can not be null")
				.put(mid, "mid can not be null")
				.put(did, "did can not be null"))) {
			return;
		}
	
		List<SystemJpushUrlInfoModel> listsjuim=SystemJpushUrlInfoModel.dao.find("select * from y_system_jpush_urlinfo where 1=1 and  groupid=?", groupid);
		SystemJpushUserReadInfoModel sjurim=null;
		int typedync=0,typeinfo=0,typefrends=0,typebuss=0;
		if(listsjuim!=null)
		{
			for(SystemJpushUrlInfoModel sjuim:listsjuim)
			{
				sjurim=SystemJpushUserReadInfoModel.dao.findFirst("select * from y_system_jpush_usrreadinfo where  1=1 and jpushids=?", sjuim.getStr(SystemJpushUrlInfoModel.ID));
				if(sjurim==null)
				{
					if(sjuim.getInt(SystemJpushUrlInfoModel.TYPE)==1)
					{
						typedync++;
					}else if(sjuim.getInt(SystemJpushUrlInfoModel.TYPE)==2)
					{
						typeinfo++;
					}else if(sjuim.getInt(SystemJpushUrlInfoModel.TYPE)==3)
					{
						typefrends++;
					}else if(sjuim.getInt(SystemJpushUrlInfoModel.TYPE)==4)
					{
						typebuss++;
					}
				}
			}
		}
		SystemPushInfoCountEntey sce=new SystemPushInfoCountEntey();
		sce.setN0(typedync);
		sce.setN1(typeinfo);
		sce.setN2(typefrends);
		sce.setN3(typebuss);

		DatumResponse response = new DatumResponse();
		response.setCode(listsjuim != null ? Code.SUCCESS : Code.ERROR);
		response.setMessage(listsjuim != null ? "SUCCESS" : "没有对应详情");
		response.setDatum(sce);
		renderJson(response);
	}
	/**
	 * 版本更新检查*Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR
	 */
	@ActionKey("/api/version/check")
	public void checkVersion() {
		String version = getPara("version");// 版本号
		String client = getPara("client"); // 终端类型, 可选值有android, iphone
		if (!notNull(Require.me().put(version, "version can not be null")
				.put(client, "client can not be null"))) {
			return;
		}
		// 检查值是否有效
		if (!Version.checkType(client)) {
			renderArgumentError("client is invalid");
			return;
		}

		Version result = VersionManager.me().check(version, client);
		DatumResponse response = new DatumResponse(result);
		if (result == null) {
			response.setCode(Code.FAIL);// 表示无更新
		}

		renderJson(response);

	}
}
